<template>
  <div class="app-container">
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="8">
            <el-form-item label="关 键 字">
              <el-input style="width: 95%" v-model="searchObj.keyword" placeholder="食物名"></el-input>
            </el-form-item>
          </el-col>
          
        </el-row>
        <el-row style="display:flex">
          <el-button type="primary" :disabled="$hasBP('bnt.foodDetail.list')  === false" icon="el-icon-search" size="mini"  @click="fetchData()">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
        </el-row>
      </el-form>
    </div>

    <!-- 工具条 -->
    <div class="tools-div">
      <el-button type="success" :disabled="$hasBP('bnt.foodDetail.add')  === false" icon="el-icon-plus" size="mini" @click="add">添 加</el-button>
    </div>

	<!-- 列表 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%;margin-top: 10px;">
      <el-table-column type="expand">
      <template slot-scope="props">
        <el-form label-position="left" inline class="demo-table-expand">
          <el-form-item label="食物名">
            <span>{{ props.row.foodName }}</span>
          </el-form-item>
          <el-form-item label="水分">
            <span>{{ props.row.waterContent }}</span>
          </el-form-item>
          <el-form-item label="能量">
            <span>{{ props.row.energy }}</span>
          </el-form-item>
          <el-form-item label="蛋白质">
            <span>{{ props.row.protein }}</span>
          </el-form-item>
          <el-form-item label="脂肪">
            <span>{{ props.row.fat }}</span>
          </el-form-item>
          <el-form-item label="碳水化合物">
            <span>{{ props.row.carbohydrate }}</span>
          </el-form-item>
          <el-form-item label="膳食纤维">
            <span>{{ props.row.dietaryFiber }}</span>
          </el-form-item>
          <el-form-item label="胆固醇">
            <span>{{ props.row.cholesterol }}</span>
          </el-form-item>
          <el-form-item label="灰分">
            <span>{{ props.row.ashContent }}</span>
          </el-form-item>
          <el-form-item label="钙">
            <span>{{ props.row.calcium }}</span>
          </el-form-item>
          <el-form-item label="磷">
            <span>{{ props.row.phosphorus }}</span>
          </el-form-item>
          <el-form-item label="钾">
            <span>{{ props.row.potassium }}</span>
          </el-form-item>
          <el-form-item label="钠">
            <span>{{ props.row.sodium }}</span>
          </el-form-item>
          <el-form-item label="镁">
            <span>{{ props.row.magnesium }}</span>
          </el-form-item>
          <el-form-item label="铁">
            <span>{{ props.row.iron }}</span>
          </el-form-item>
          <el-form-item label="锌">
            <span>{{ props.row.zinc }}</span>
          </el-form-item>
          <el-form-item label="硒">
            <span>{{ props.row.selenium }}</span>
          </el-form-item>
          <el-form-item label="铜">
            <span>{{ props.row.copper }}</span>
          </el-form-item>
          <el-form-item label="锰">
            <span>{{ props.row.manganese }}</span>
          </el-form-item>
          <el-form-item label="碘">
            <span>{{ props.row.iodine }}</span>
          </el-form-item>
          <el-form-item label="总维生素A">
            <span>{{ props.row.totalVitaminA }}</span>
          </el-form-item>
          <el-form-item label="视黄醇">
            <span>{{ props.row.retinol }}</span>
          </el-form-item>
          <el-form-item label="胡萝卜素">
            <span>{{ props.row.carotenes }}</span>
          </el-form-item>
          <el-form-item label="维生素D">
            <span>{{ props.row.vd }}</span>
          </el-form-item>
          <el-form-item label="维生素E">
            <span>{{ props.row.ve }}</span>
          </el-form-item>
          <el-form-item label="维生素K">
            <span>{{ props.row.vk }}</span>
          </el-form-item>
          <el-form-item label="维生素B1(硫胺素)">
            <span>{{ props.row.vb1 }}</span>
          </el-form-item>
          <el-form-item label="维生素B2(核黄素)">
            <span>{{ props.row.vb2 }}</span>
          </el-form-item>
          <el-form-item label="烟酸">
            <span>{{ props.row.nicotinicAcid }}</span>
          </el-form-item>
          <el-form-item label="维生素B6">
            <span>{{ props.row.vb6 }}</span>
          </el-form-item>
          <el-form-item label="维生素B12">
            <span>{{ props.row.vb12 }}</span>
          </el-form-item>
          <el-form-item label="叶酸">
            <span>{{ props.row.folicAcid }}</span>
          </el-form-item>
          <el-form-item label="胆碱">
            <span>{{ props.row.choline }}</span>
          </el-form-item>
          <el-form-item label="生物素">
            <span>{{ props.row.biotin }}</span>
          </el-form-item>
          <el-form-item label="泛酸">
            <span>{{ props.row.pantothenate }}</span>
          </el-form-item>
        </el-form>
      </template>
    </el-table-column>
      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="foodName" label="食物名" />
      <el-table-column prop="typeName" label="分类" />
    
      <el-table-column label="操作"  align="center" fixed="right" width="130">
        <template slot-scope="scope">
          <el-button type="primary" :disabled="$hasBP('bnt.foodDetail.update')  === false" icon="el-icon-edit" size="mini" @click="edit(scope.row.id)" title="修改"/>
          <el-button type="danger" :disabled="$hasBP('bnt.foodDetail.remove')  === false" icon="el-icon-delete" size="mini" @click="removeDataById(scope.row.id)" title="删除" />
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />

    <el-dialog title="添加/修改" :visible.sync="dialogVisible" width="40%" >
      <el-form ref="dataForm" :model="foodDetail"  label-width="100px" size="small" style="padding-right: 40px;">
        <el-form-item label="食物名" prop="foodName">
          <el-input v-model="foodDetail.foodName"/>
        </el-form-item>
        <el-form-item  label="分类" prop="typeName">
          <el-select @change="change($event)"  v-model="foodDetail.typeName" placeholder="请选择">
            <el-option-group
              v-for="group in typeAll"
              :key="group.id"
              :label="group.typeName">
              <el-option
                v-for="item in group.children"
                :key="item.id"
                :label="item.typeName"
                :value="item.id">
              </el-option>
            </el-option-group>
          </el-select>
        </el-form-item>
        
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small" icon="el-icon-refresh-right">取 消</el-button>
        <el-button type="primary" icon="el-icon-check" @click="saveOrUpdate()" size="small">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import api from '@/api/system/foodDetail'
import typeApi from '@/api/system/foodType'

export default {
  data() {
    return {
      listLoading: false, // 数据是否正在加载
      list: null, // 列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 5, // 每页记录数
      searchObj: {}, // 查询表单对象
      typeAll:'',
      createTimes: [],

      dialogVisible: false,
      foodDetail: {},

      dialogRoleVisible: false,
      isIndeterminate: false, // 是否是不确定的
      checkAll: false // 是否全选
    }
  },
  created() {
    //调用列表方法
    this.fetchData()
    this.getAll()
  },
  methods:{
    change(e) {
      // 通过打印可以查看是否能拿到对应值!!!
      console.log(this.foodDetail)
      this.foodDetail.typeId=e;
      console.log(this.foodDetail)
    },
    /*
    全选勾选状态发生改变的监听
    */
    handleCheckAllChange (value) {// value 当前勾选状态true/false
      // 如果当前全选, userRoleIds就是所有角色id的数组, 否则是空数组
      this.userRoleIds = value ? this.allRoles.map(item => item.id) : []
      // 如果当前不是全选也不全不选时, 指定为false
      this.isIndeterminate = false
    },

    /*
    角色列表选中项发生改变的监听
    */
    handleCheckedChange (value) {
      const {userRoleIds, allRoles} = this
      this.checkAll = userRoleIds.length === allRoles.length && allRoles.length>0
      this.isIndeterminate = userRoleIds.length>0 && userRoleIds.length<allRoles.length
    },
    //删除
    removeDataById(id) {
        this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //调用方法删除
          api.removeById(id)
            .then(response => {
              //提示
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              //刷新
              this.fetchData()
          })
        })
    },
    //根据id查询，数据回显
    edit(id) {
      console.log(this.typeAll)
      //弹出框
      this.dialogVisible = true
      //调用接口查询
      api.getUserId(id)
        .then(response => {
          this.foodDetail = response.data
        })
    },
    //添加或者修改方法
    saveOrUpdate() {
      if(!this.foodDetail.id) {
        this.save()
      } else {
        this.update()
      }
    },
    //修改
    update() {
      api.update(this.foodDetail)
        .then(response => {
          //提示
          this.$message.success('操作成功')
          //关闭弹框
          this.dialogVisible = false
          //刷新
          this.fetchData()
        })
    },
    //添加
    save() {
      api.save(this.foodDetail)
        .then(response => {
          //提示
          this.$message.success('操作成功')
          //关闭弹框
          this.dialogVisible = false
          //刷新
          this.fetchData()
        })
    },
    //添加弹框的方法
    add() {
      this.dialogVisible = true
      this.foodDetail = {}
    },
    // 重置查询表单
    resetData() {
      console.log('重置查询表单')
      this.searchObj = {}
      this.createTimes = []
      this.fetchData()
    },
    //列表
    fetchData(page=1) {
      this.page = page
      if(this.createTimes && this.createTimes.length==2) {
        this.searchObj.createTimeBegin = this.createTimes[0]
        this.searchObj.createTimeEnd = this.createTimes[1]
      }
      api.getPageList(this.page,this.limit,this.searchObj)
        .then(response => {
          this.list = response.data.records
          this.total = response.data.total
        })
    },
    // 获取全部分类
    getAll(){
      typeApi.getAll().then(response => {
          this.typeAll = response.data
        }
      )
    }
  }
}
</script>
<style scoped>
/deep/ .el-table .el-table__body td .cell:empty::after,
  .el-table .el-table__body td .cell > span:empty::after,
  .el-table .el-table__body td .cell .approval:empty::after,
  .el-table .el-table__body td .cell div:empty::after,
  .el-table .el-table__body td .cell .line-limit-length:empty::after {
    content: "暂未填写";
  }
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>